package com.gqr.springcloud;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;

import javax.annotation.Resource;
import java.time.ZonedDateTime;

@Configuration
public class GatewayConfiguration {
    @Resource
    private TimerFilter timerFilter;

    @Resource
    private AuthFilter authFilter;
    @Bean
    @Order
    public RouteLocator customizedRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/java/**")
                        .and().method(HttpMethod.GET)
                        .and().header("name")
                        .filters(f -> f.stripPrefix(1)
                                .addResponseHeader("java-param", "gateway-config")
                                .filters(timerFilter)
                                .filters(authFilter)
                        )
                        .uri("lb://FEIGN-CLIENT")
                )
                // after 作用的路由规则只在规定的时间以后生效  before 是之前  between
                .route(r->r.path("/seckill/**")
                        .and().after(ZonedDateTime.now().plusMinutes(1))
//                        .and().before()
                        .filters(f -> f.stripPrefix(1))
                        .uri("lb://FEIGN-CLIENT")
                )
                .build();

    }
}
